//
//  TBRBusinessRentDetailCViewController.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/14.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRGetRentDetailMessageC.h"
#import "TBROnCarPlaceC.h"
#import "TBRRentDetailController.h"

#import "TBRRentDetailOnOffCarCell.h"
#import "TBRRentDetailCarPersonCell.h"
#import "TBRSelfDriveDetailOnOffCarCell.h"

#import "TBRSelfDrivePickerC.h"
#import "TBRTextPickerC.h"
#import "TBRRecevePlaneDurationPickerC.h"

#import "TBRRentPersonMessageC.h"
#import "UINavigationController+SXFullScreen.h"
#import "YXDateHelpObject.h"
#import "NSString+Rect.h"
#import "TBRLoginController.h"
#import "BaseNavigationController.h"
#import "TBRGetPlaneMessageModel.h"
#import "TBRGetPlaneMessageC.h"
#import "BRPickerView.h"

@interface TBRGetRentDetailMessageC ()<UITableViewDelegate,UITableViewDataSource,TBRRentDetailOnOffCarCellDelegate,TBRRentDetailCarPersonCellDelegate,TBRSelfDriveDetailOnOffCarCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;


// MARK: - 自驾的数据----------------------
@property (nonatomic,copy)NSString *timeDuration;//间隔时间----


@end

@implementation TBRGetRentDetailMessageC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setupUI];
}
- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRRentDetailOnOffCarCell" bundle:nil] forCellReuseIdentifier:@"TBRRentDetailOnOffCarCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRRentDetailCarPersonCell" bundle:nil] forCellReuseIdentifier:@"TBRRentDetailCarPersonCellId"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRSelfDriveDetailOnOffCarCell" bundle:nil] forCellReuseIdentifier:@"TBRSelfDriveDetailOnOffCarCellId"];
}

- (void)setupNavItem {
    NSArray *titleArray = @[@"商务接送",@"商务接送",@"接机",@"送机",@"包车",@"自驾租车"];
    self.titleText = titleArray[_selIndex];
    [self ISG_NavigationBarWithBackButtonTitle:nil leftImageNamed:nil andAction:@selector(backBtnClick)];
    self.sx_disableInteractivePop = YES;
}

- (void)backBtnClick {
    __weak typeof(self) weakSelf = self;
    [self showBackBtnClickAlertShowMessage:nil block:^{
        if (weakSelf.getRentDetailMessageCBlock) {
//            [TBRRentAllMessageModel shareAllMessageModel].offPlace = @"";
            [[TBRRentAllMessageModel shareAllMessageModel] resetRentMessage];
            weakSelf.getRentDetailMessageCBlock();
        }
    }];
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TBRRentDetailOnOffCarCellId";
    if (indexPath.row == 0) {
        iden = @"TBRRentDetailOnOffCarCellId";
        if (self.selIndex == 5) {
            iden = @"TBRSelfDriveDetailOnOffCarCellId";
        }
    }else {
        iden = @"TBRRentDetailCarPersonCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        if (self.selIndex == 5) {
            TBRSelfDriveDetailOnOffCarCell *cellSelfDrive = (TBRSelfDriveDetailOnOffCarCell *)cell;
            self.timeDuration = [NSString stringWithFormat:@"%ld",[AppSetting shareAppSetting].timeDuration];
            [cellSelfDrive refreshUI];
            cellSelfDrive.delegate = self;
            return;
        }
        TBRRentDetailOnOffCarCell *detailOnOffCarCell = (TBRRentDetailOnOffCarCell *)cell;
        detailOnOffCarCell.selIndex = self.selIndex;
        [detailOnOffCarCell refreshUI];
        detailOnOffCarCell.delegate = self;
    }else {
        TBRRentDetailCarPersonCell *detailCarCell = (TBRRentDetailCarPersonCell *)cell;
        [detailCarCell.remarkText addTextViewEndEvent:^(BRPlaceholderTextView *text) {
            [TBRRentAllMessageModel shareAllMessageModel].remark = text.text;
        }];
        [detailCarCell.remarkText addMaxTextLengthWithMaxLength:50 andEvent:^(BRPlaceholderTextView *text) {
        }];
        detailCarCell.remarkText.placeholder = @"选填,可告知您的其他需求(最多50字)";
        
        if (self.selIndex == 5) {
            detailCarCell.view_selfDrive.hidden = NO;
            if ([HelpObject isBlankString:[TBRRentAllMessageModel shareAllMessageModel].showPersonMessage]) {
                [detailCarCell.lbe_personName setText:@"选乘车人"];
                
            }else {
                [detailCarCell.lbe_personName setText:[TBRRentAllMessageModel shareAllMessageModel].showPersonMessage];
                
            }
        }else {
            detailCarCell.view_selfDrive.hidden = YES;
            if (self.selIndex != 2) {
                NSString *onCarTime = [HelpObject isBlankString:[TBRRentAllMessageModel shareAllMessageModel].onCarTime] ? @"预约时间" : [TBRRentAllMessageModel shareAllMessageModel].onCarTime;
                [detailCarCell.btn_yuYueTime setTitle:onCarTime forState:UIControlStateNormal];
            }else {
                NSString *timeDuration = [TBRRentAllMessageModel shareAllMessageModel].selPlaneMessageModel.receptPlaneDuration;
                NSString *tmpTimeDuration = [NSString stringWithFormat:@"航班到后%@分钟用车",timeDuration];
                NSMutableAttributedString *tmpTimeMutableAttributedDuration = [NSString changeTextColor:tmpTimeDuration OneColor:[UIColor blackColor] TwoColor:[UIColor redColor] Range:NSMakeRange(4, 4)];
                [detailCarCell.btn_yuYueTime setAttributedTitle:tmpTimeMutableAttributedDuration forState:UIControlStateNormal];
            }
            NSString *personName = [HelpObject isBlankString:[TBRRentAllMessageModel shareAllMessageModel].showPersonMessage] ? @"选乘车人" : [TBRRentAllMessageModel shareAllMessageModel].showPersonMessage;
            [detailCarCell.btn_personName setTitle:personName forState:UIControlStateNormal];
        }
        detailCarCell.delegate = self;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && self.selIndex == 5) {
        return 199;
    }
    return indexPath.row == 0 ? 113 : 405;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)TBRRentDetailOnOffCarCellOnCarBtnClick {
    [self onOffCarPlaceBtnClick:YES];
}

- (void)TBRRentDetailOnOffCarCellOffCarBtnClick {
    [self onOffCarPlaceBtnClick:NO];
}

- (void)TBRRentDetailOnOffCarCellSelPlaneBtnClick {
    //输入航班号---
    TBRGetPlaneMessageC *getPlaneMessageC = [[TBRGetPlaneMessageC alloc] init];
    __weak typeof(self) weakSelf = self;
    getPlaneMessageC.getPlaneMessageCBlock = ^(TBRGetPlaneMessageModel *selPlaneModel) {
        //取出航班信息
        selPlaneModel.showPlaneStr = [NSString stringWithFormat:@"%@ %@到达%@航站楼%@",selPlaneModel.showselDateStr,selPlaneModel.planArriveTime,selPlaneModel.to,selPlaneModel.toTerminal];
        [TBRRentAllMessageModel shareAllMessageModel].selPlaneMessageModel = selPlaneModel;
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:getPlaneMessageC animated:YES];
}

// MARK: - 自驾
- (void)selfDriveAppointCellBtnClick:(NSInteger)tag {
    TBRRentAllMessageModel *allMessageModel = [TBRRentAllMessageModel shareAllMessageModel];
    if (tag == 10) {
        [self selSelfDriveTimePicker:YES];
    }else if (tag == 20) {
        [self selSelfDriveTimePicker:NO];
    }else if (tag == 30) {
        //根据是否打开开关来选择-------
        if (allMessageModel.isGetCarFromShop) {
            [self selSelfDriveOnOffPlaceClick:YES];
            return;
        }
        [self onOffCarPlaceBtnClick:YES];
    }else if (tag == 40) {
        if (allMessageModel.isReBackCarToShop) {
            [self selSelfDriveOnOffPlaceClick:NO];
            return;
        }
        [self onOffCarPlaceBtnClick:NO];
    }
}


// MARK: - 自驾类型的时间选择------------------------------
- (void)selSelfDriveTimePicker:(BOOL) isStartTime {
    TBRSelfDriveDetailOnOffCarCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    __weak typeof(self) weakSelf = self;
    __weak TBRSelfDriveDetailOnOffCarCell *weakCellSelfDriveAppoint = cell;
    TBRSelfDrivePickerC *pickerC = [[TBRSelfDrivePickerC alloc] init];
    pickerC.isStartTime = isStartTime;
    __weak TBRSelfDrivePickerC *weakPickerC = pickerC;
    pickerC.pickerViewCancleBtnBlock = ^{
        [weakPickerC removeFromParentViewController];
        [weakPickerC.view removeFromSuperview];
    };
    pickerC.pickerViewSureBtnBlock = ^(NSString *dateStr, NSString *hourStr, NSString *minStr,NSString *weekStr) {
        [weakPickerC removeFromParentViewController];
        [weakPickerC.view removeFromSuperview];
        NSString *startTimeStr = [TBRRentAllMessageModel shareAllMessageModel].startTime;
        NSString *endTimeStr = [TBRRentAllMessageModel shareAllMessageModel].endTime;
        NSDate *startDate = [[YXDateHelpObject manager] getDataFromStrFormat:@"yyyy/M/d H:m" String:startTimeStr];
        NSDate *endDate = [[YXDateHelpObject manager] getDataFromStrFormat:@"yyyy/M/d H:m" String:endTimeStr];
        
        NSInteger tmpDuration = [YXDateHelpObject numberOfDaysWithFromDate:startDate toDate:endDate];
        [AppSetting shareAppSetting].timeDuration = tmpDuration;
        weakSelf.timeDuration = [NSString stringWithFormat:@"%ld",[AppSetting shareAppSetting].timeDuration];
        [TBRRentAllMessageModel shareAllMessageModel].startDateTime = [[YXDateHelpObject manager] getCurentTime:@"M-dd" dateN:startDate];
        [TBRRentAllMessageModel shareAllMessageModel].endDateTime = [[YXDateHelpObject manager] getCurentTime:@"M-dd" dateN:endDate];
        
        if (isStartTime) {
            if (tmpDuration < 1) {
                [AppSetting shareAppSetting].timeDuration = 3;
                weakSelf.timeDuration = [NSString stringWithFormat:@"%ld",[AppSetting shareAppSetting].timeDuration];
                NSDate *newEndDate = [[YXDateHelpObject manager] getEarlyOrLaterDate:startDate LeadTime:3 Type:2];
                [TBRRentAllMessageModel shareAllMessageModel].endDateTime = [[YXDateHelpObject manager] getCurentTime:@"M-dd" dateN:newEndDate];
                
                [TBRRentAllMessageModel shareAllMessageModel].endWeakHour = [NSString stringWithFormat:@"%@ %@:%@",weekStr,hourStr,minStr];
                weakCellSelfDriveAppoint.lbe_endWeekHour.text = [TBRRentAllMessageModel shareAllMessageModel].endWeakHour;
                weakCellSelfDriveAppoint.lbe_endDate.text = [TBRRentAllMessageModel shareAllMessageModel].endDateTime;
                [TBRRentAllMessageModel shareAllMessageModel].endTime = [[YXDateHelpObject manager] getCurentTime:@"yyyy/MM/dd H:mm" dateN:newEndDate];
            }
            
            [TBRRentAllMessageModel shareAllMessageModel].startWeakHour = [NSString stringWithFormat:@"%@ %@:%@",weekStr,hourStr,minStr];
            weakCellSelfDriveAppoint.lbe_StartWeekHour.text = [TBRRentAllMessageModel shareAllMessageModel].startWeakHour;
            weakCellSelfDriveAppoint.lbe_StartDate.text = [TBRRentAllMessageModel shareAllMessageModel].startDateTime;
        }else {
            [TBRRentAllMessageModel shareAllMessageModel].endWeakHour = [NSString stringWithFormat:@"%@ %@:%@",weekStr,hourStr,minStr];
            weakCellSelfDriveAppoint.lbe_endWeekHour.text = [TBRRentAllMessageModel shareAllMessageModel].endWeakHour;
            weakCellSelfDriveAppoint.lbe_endDate.text = [TBRRentAllMessageModel shareAllMessageModel].endDateTime;
        }
        weakCellSelfDriveAppoint.lbe_timeDuration.text = weakSelf.timeDuration;
    };
    [self.navigationController addChildViewController:pickerC];
    [self.navigationController.view addSubview:pickerC.view];
    pickerC.view.frame = CGRectMake(0, 0, Width, Height + 49);
}


// MARK: - 自驾后台数据位置选择---------
- (void)selSelfDriveOnOffPlaceClick:(BOOL)isGet {
    TBRSelfDriveDetailOnOffCarCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    __weak TBRSelfDriveDetailOnOffCarCell *weakCellSelfDriveAppoint = cell;
    NSString *pointStrTmp = isGet ? @"请选择取车地点" : @"请选择还车地点";
    [BRStringPickerView showStringPickerWithTitle:pointStrTmp dataSource:self.onOffPlaceArray defaultSelValue:weakCellSelfDriveAppoint.lbe_offCarPlace.text resultBlock:^(id selectValue) {
        if (isGet) {
            weakCellSelfDriveAppoint.lbe_onCarPlace.text = selectValue;
            [TBRRentAllMessageModel shareAllMessageModel].isGetCarFromShopOnPlace = selectValue;
        }else {
            weakCellSelfDriveAppoint.lbe_offCarPlace.text = selectValue;
            [TBRRentAllMessageModel shareAllMessageModel].offPlace = selectValue;
        }
    }];
}

// MARK: - 上下车选择-----------
- (void)onOffCarPlaceBtnClick:(BOOL)isOn {
    TBRRentDetailOnOffCarCell *rentDetailOnOffCarCell  = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    __weak TBRRentDetailOnOffCarCell *weakRentDetailOnOffCarCell = rentDetailOnOffCarCell;
    TBROnCarPlaceC *onCarPlaceC = [[TBROnCarPlaceC alloc] init];
    onCarPlaceC.isOnCarPlace = isOn;
    onCarPlaceC.onCarPlaceBlock = ^(TBROnCarPlaceModel *carPlaceModel) {
        if (isOn) {
            weakRentDetailOnOffCarCell.lbe_onCarPlace.text = carPlaceModel.titleStr;
            [TBRRentAllMessageModel shareAllMessageModel].onPlace = carPlaceModel.titleStr;
        }else {
            weakRentDetailOnOffCarCell.lbe_offCarPlace.text = carPlaceModel.titleStr;
            [TBRRentAllMessageModel shareAllMessageModel].offPlace = carPlaceModel.titleStr;
        }
    };
    onCarPlaceC.navigationItem.title = isOn ? @"请选择上车地点" : @"请选择下车地点";
    [[HelpObject getCurrentNav] pushViewController:onCarPlaceC animated:NO];
}

- (void)rentDetailCarPersonCellTimePersonBtnClick:(NSInteger)tag {
    if (tag == 10) {
        //选择时间
        
        if (self.selIndex == 2) {
            //选择接机时间段---
            [self selReceptPlaneTimeDuration];
            return;
        }
        
        [self selBuinessRentAppointmentTimeBtnClick];
    }else {
        //选择人
        [self selPersonMessage];
    }
}

// MARK: - 普通类型预定时间选择
- (void)selBuinessRentAppointmentTimeBtnClick {
    //选择时间
    TBRTextPickerC *pickerC = [[TBRTextPickerC alloc] init];
    __weak TBRTextPickerC *weakPickerC = pickerC;
    __weak typeof(self) weakSelf = self;
    pickerC.pickerViewCancleBtnBlock = ^{
        [weakPickerC removeFromParentViewController];
        [weakPickerC.view removeFromSuperview];
    };
    pickerC.pickerViewSureBtnBlock = ^(NSString *dateStr, NSString *hourStr, NSString *minStr) {
        NSString *tmpDateStr = [dateStr componentsSeparatedByString:@" "].firstObject;
        tmpDateStr = [NSString stringWithFormat:@"%@ %@:%@",tmpDateStr,hourStr,minStr];
        [TBRRentAllMessageModel shareAllMessageModel].onCarTime = tmpDateStr;
        [weakSelf.tableView reloadData];
        [weakPickerC removeFromParentViewController];
        [weakPickerC.view removeFromSuperview];
    };
    [self.navigationController addChildViewController:pickerC];
    [self.navigationController.view addSubview:pickerC.view];
    pickerC.view.frame = [UIApplication sharedApplication].keyWindow.frame;
    pickerC.view.frame = CGRectMake(0, 0, Width, Height + 49);
}

// MARK: - 航班时间段选择----
- (void)selReceptPlaneTimeDuration {
    //选择时间
    TBRRecevePlaneDurationPickerC *pickerC = [[TBRRecevePlaneDurationPickerC alloc] init];
    __weak TBRRecevePlaneDurationPickerC *weakPickerC = pickerC;
    __weak typeof(self) weakSelf = self;
    pickerC.pickerViewCancleBtnBlock = ^{
        [weakPickerC removeFromParentViewController];
        [weakPickerC.view removeFromSuperview];
    };
    pickerC.pickerViewSureBtnBlock = ^(NSString *duration) {
        [TBRRentAllMessageModel shareAllMessageModel].selPlaneMessageModel.receptPlaneDuration = duration;
        [weakSelf.tableView reloadData];
        [weakPickerC removeFromParentViewController];
        [weakPickerC.view removeFromSuperview];
    };
    [self.navigationController addChildViewController:pickerC];
    [self.navigationController.view addSubview:pickerC.view];
    pickerC.view.frame = CGRectMake(0, 0, Width, Height + 49);
}

// MARK: - 选择乘车人信息-----------------
- (void)selPersonMessage {
    __weak typeof(self) weakSelf = self;
    TBRRentPersonMessageC *rentPersonMC = [[TBRRentPersonMessageC alloc] init];
    [[HelpObject getCurrentNav] pushViewController:rentPersonMC animated:YES];
    //确认按钮点击时候吧值传递回来
    rentPersonMC.rentPersonMessageBlock = ^(TBRRentPersonInfoModel *rentPersonInfoModel) {
        //把数值存起来-----------------------------
        if (![HelpObject isBlankString:rentPersonInfoModel.personNameCall]) {
            //显示称呼
            [TBRRentAllMessageModel shareAllMessageModel].showPersonMessage = rentPersonInfoModel.personNameCall;
            [TBRRentAllMessageModel shareAllMessageModel].showPersonMessageDetail = [NSString stringWithFormat:@"%@ %@",rentPersonInfoModel.personNameCall,rentPersonInfoModel.phoneNum];
        }else {
            //显示人名
            [TBRRentAllMessageModel shareAllMessageModel].showPersonMessage = rentPersonInfoModel.personName;
            [TBRRentAllMessageModel shareAllMessageModel].showPersonMessageDetail = [NSString stringWithFormat:@"%@ %@",rentPersonInfoModel.personName,rentPersonInfoModel.phoneNum];
        }
        [weakSelf.tableView reloadData];
//        NSString *personMessageStr = [NSString stringWithFormat:@"%@(%@)               %@",[HelpObject changeNull:rentPersonInfoModel.personNameCall],[HelpObject changeNull:rentPersonInfoModel.personName],[HelpObject changeNull:rentPersonInfoModel.phoneNum]];
//        [weakRentDetailCarPersonCell.btn_personName setTitle:personMessageStr forState:UIControlStateNormal];
    };
}

// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    [TBRRentAllMessageModel shareAllMessageModel].selPlaneMessageModel.receptPlaneDuration = @"10";
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    
}

- (IBAction)yuYueBtnClick:(UIButton *)sender {
    
    //判断是否登录
    NSString *isLogin = [HelpObject changeNull:[AppSetting shareAppSetting].isLogin];
    if (![isLogin isEqualToString:@"1"]) {
        TBRLoginController *loginC = [[TBRLoginController alloc] init];
        BaseNavigationController *loginNav = [[BaseNavigationController alloc] initWithRootViewController:loginC];
        [self presentViewController:loginNav animated:YES completion:nil];
        return;
    }
    
    //判断数据是否填写完整-----
    BOOL isCorrect = [self judgeIsCorrect];
    if (!isCorrect) {
        return;
    }
    TBRRentDetailController *rentDetailC = [[TBRRentDetailController alloc] init];
    rentDetailC.selectedIndex = self.selIndex;
    [self.navigationController pushViewController:rentDetailC animated:YES];
}

// MARK: - 判断是否合法-----
- (BOOL)judgeIsCorrect {
    TBRRentAllMessageModel *allMessageModel = [TBRRentAllMessageModel shareAllMessageModel];
    if (self.selIndex == 5) {
        if (allMessageModel.isGetCarFromShop && [HelpObject isBlankString:allMessageModel.isGetCarFromShopOnPlace]) {
            [self setNoticeText:@"请先选择取车地点"];
            return NO;
        }
        if (!allMessageModel.isGetCarFromShop && [HelpObject isBlankString:allMessageModel.onPlace]) {
            [self setNoticeText:@"请先选择取车地点"];
            return NO;
        }
        if ([HelpObject isBlankString:allMessageModel.offPlace]) {
            [self setNoticeText:@"请先选择还车地点"];
            return NO;
        }
        if ([HelpObject isBlankString:allMessageModel.showPersonMessage]) {
            [self setNoticeText:@"请先选择乘车人信息"];
            return NO;
        }
        return YES;
    }else if (self.selIndex == 2) {
        if ([HelpObject isBlankString:allMessageModel.showPersonMessage]) {
            [self setNoticeText:@"请先选择乘车人信息"];
            return NO;
        }
        return YES;
    }
    if ([HelpObject isBlankString:allMessageModel.onCarTime]) {
        [self setNoticeText:@"请先预约时间"];
        return NO;
    }
    else if ([HelpObject isBlankString:allMessageModel.showPersonMessage]) {
        [self setNoticeText:@"请先选择乘车人信息"];
        return NO;
    }
    return YES;
}

@end
